package com.coai.message.controller

import com.coai.common.dto.ApiResponse
import com.coai.message.dto.MessageDto
import com.coai.message.service.AIMessageService
import kotlinx.coroutines.runBlocking
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/ai-messages")
class AIMessageController(
    private val aiMessageService: AIMessageService
) {
    
    /**
     * 请求AI会议总结
     */
    @PostMapping("/meeting-summary")
    fun requestMeetingSummary(
        @RequestParam conversationId: String,
        @RequestParam startTime: String,
        @RequestParam endTime: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<MessageDto> = runBlocking {
        val summary = aiMessageService.analyzeMeetingContent(conversationId, startTime, endTime)
        ApiResponse.success(summary, "会议总结已生成")
    }
    
    /**
     * 从消息中提取任务
     */
    @PostMapping("/extract-tasks")
    fun extractTasks(
        @RequestParam conversationId: String,
        @RequestBody messageIds: List<String>,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<List<MessageDto>> = runBlocking {
        val tasks = aiMessageService.extractTasks(conversationId, messageIds)
        ApiResponse.success(tasks, "已提取${tasks.size}个任务")
    }
    
    /**
     * 生成内容（商品文案、营销文案等）
     */
    @PostMapping("/generate-content")
    fun generateContent(
        @RequestParam conversationId: String,
        @RequestParam contentType: String,
        @RequestBody params: Map<String, Any>,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<MessageDto> = runBlocking {
        val content = aiMessageService.generateContent(conversationId, contentType, params)
        ApiResponse.success(content, "内容已生成")
    }
    
    /**
     * 获取销售话术建议
     */
    @PostMapping("/sales-script")
    fun getSalesScript(
        @RequestParam conversationId: String,
        @RequestParam customerId: String,
        @RequestBody context: Map<String, Any>,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<MessageDto> = runBlocking {
        val script = aiMessageService.suggestSalesScript(conversationId, customerId, context)
        ApiResponse.success(script, "话术建议已生成")
    }
    
    /**
     * 智能语义搜索
     */
    @GetMapping("/smart-search")
    fun smartSearch(
        @RequestParam conversationId: String,
        @RequestParam query: String,
        @RequestParam(defaultValue = "true") useSemanticSearch: Boolean,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<List<Any>> = runBlocking {
        val messages = aiMessageService.smartSearch(conversationId, query, useSemanticSearch)
        ApiResponse.success(messages.map { mapOf("id" to it.id, "content" to it.content) })
    }
    
    /**
     * 翻译消息
     */
    @PostMapping("/translate")
    fun translateMessage(
        @RequestParam messageId: String,
        @RequestParam targetLanguage: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<String> = runBlocking {
        // TODO: 获取message并翻译
        ApiResponse.success("", "翻译完成")
    }
}
